package com.rocching.easychat.ui;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;

import com.rocching.easychat.MainActivity;
import com.rocching.easychat.R;
import com.rocching.easychat.core.UserIdentityContext;
import com.rocching.easychat.db.LocalUserService;
import com.rocching.easychat.model.UserModel;
import com.rocching.easychat.util.HttpUtil;

import java.util.Objects;

public class WelcomeActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);

        init();
    }

    private void init() {
        int delayMillis = 2000;
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                goTo();
            }
        }, delayMillis);
    }

    private void goTo() {
        Class<?> tClass;
        UserModel model = LocalUserService.getInstance().get(UserModel.class);
        if (Objects.isNull(model)) {
            tClass = LoginActivity.class;
        } else {
            tClass = MainActivity.class;

            UserIdentityContext.set(model);
            HttpUtil.setBearerValue(model.getToken());
        }
        Intent intent = new Intent(this, tClass);
        startActivity(intent);
        finish();
    }
}